package com.yjt.admin.system.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.admin.system.dao.RoleMapper;
import com.yjt.admin.system.entity.Role;
import com.yjt.admin.system.form.EmpRoleForm;
import com.yjt.admin.system.form.RoleForm;
import com.yjt.admin.system.form.RoleMenuForm;
import com.yjt.admin.system.service.EmpRoleService;
import com.yjt.admin.system.service.RoleMenuService;
import com.yjt.admin.system.service.RoleService;
import com.yjt.common.utils.ComUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2021-10-08
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private EmpRoleService empRoleService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRole(RoleForm roleForm) {
        //保存角色信息
        Role role = new Role();
        BeanUtils.copyProperties(roleForm, role);
        this.save(role);

        //保存角色与菜单之间的关系
        if(ComUtil.isNotNullList(roleForm.getMenuIdList())){
            RoleMenuForm roleMenuForm = new RoleMenuForm(role.getRoleId(),roleForm.getMenuIdList());
            roleMenuService.addOrUpdate(roleMenuForm);
        }

        //保存角色与员工之间的关系
        if(ComUtil.isNotNullList(roleForm.getEmplyIdList())){
            EmpRoleForm empRoleForm = new EmpRoleForm(role.getRoleId(),roleForm.getEmplyIdList());
            empRoleService.addEmpRole(empRoleForm);
        }
    }
}
